package com.GLM.Tools.DB.DAO;

import java.io.ByteArrayOutputStream;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.drawable.Drawable;

import com.GLM.Constants.Constant;
import com.GLM.Entity.UserInfoEntity;
import com.GLM.Tools.DB.DatabaseHelpter;

public class UserInfoDAO {
	private static Bitmap userAvatar;
	public static boolean isFirstLogin(final Context c) {
		boolean isFirstLogin = true;
		DatabaseHelpter dbHelper = DatabaseHelpter.getInstance(c);
		SQLiteDatabase db = dbHelper.getReadableDatabase();
		Cursor cursor = db.query(true, Constant.TABLE_USERINFO, new String[] { Constant.COLUMN_USERID }, null, null, null, null, null, null);
		if (cursor.getCount() > 0) {
			isFirstLogin = false;
		}
		if (cursor != null && !cursor.isClosed()) {
			cursor.close();
		}
		return isFirstLogin;
	}

	public static UserInfoEntity getUserInfo(final Context c) {
		UserInfoEntity userInfo = null;
		DatabaseHelpter dbHelper = DatabaseHelpter.getInstance(c);
		SQLiteDatabase db = dbHelper.getReadableDatabase();
		Cursor cursor = db.query(true, Constant.TABLE_USERINFO, new String[] { Constant.COLUMN_USERID, Constant.COLUMN_USERACCOUNT, Constant.COLUMN_PASSWORD, Constant.COLUMN_NICKNAME, Constant.COLUMN_EMAIL, Constant.COLUMN_LATITUDE, Constant.COLUMN_LONGITUDE,Constant.COLUMN_USERAVATARURL, Constant.COLUMN_USERAVATAR }, null,
				null, null, null, null, null);
		if (cursor.moveToFirst()) {
			userInfo = new UserInfoEntity();
			userInfo.setUserId(cursor.getInt(0));
			userInfo.setUserAccount(cursor.getString(1));
			userInfo.setUserPassword(cursor.getString(2));
			userInfo.setNickName(cursor.getString(3));
			userInfo.setEmail(cursor.getString(4));
			userInfo.setLatitude(cursor.getDouble(5));
			userInfo.setLongitude(cursor.getDouble(6));
			userInfo.setAvatarUrl(cursor.getString(7));
			byte[] imageQueried = cursor.getBlob(8);
			if (imageQueried != null) {
				Bitmap imagebitmap = BitmapFactory.decodeByteArray(imageQueried, 0, imageQueried.length);
				userInfo.setUserAvatar(imagebitmap);
			}
		}
		if (cursor != null && !cursor.isClosed()) {
			cursor.close();
		}
		return userInfo;
	}

	public static void setUserInfo(final Context c, UserInfoEntity userInfo) {
		DatabaseHelpter dbHelper = DatabaseHelpter.getInstance(c);
		SQLiteDatabase db = dbHelper.getWritableDatabase();
		Cursor cursor = db.query(true, Constant.TABLE_USERINFO, new String[] { Constant.COLUMN_USERID }, null, null, null, null, null, null);

		ContentValues values = new ContentValues();
		values.put(Constant.COLUMN_USERACCOUNT, userInfo.getUserAccount());
		values.put(Constant.COLUMN_PASSWORD, userInfo.getUserPassword());
		values.put(Constant.COLUMN_NICKNAME, userInfo.getNickName());
		values.put(Constant.COLUMN_EMAIL, userInfo.getEmail());
		values.put(Constant.COLUMN_LATITUDE, userInfo.getLatitude());
		values.put(Constant.COLUMN_LONGITUDE, userInfo.getLongitude());
		values.put(Constant.COLUMN_USERAVATARURL, userInfo.getAvatarUrl());
		Bitmap bitmap = userInfo.getUserAvatar();
		if (bitmap != null) {
			int size = bitmap.getWidth() * bitmap.getHeight() * 4;
			ByteArrayOutputStream baos = new ByteArrayOutputStream(size);
			bitmap.compress(Bitmap.CompressFormat.JPEG, 100, baos);
			byte[] imagedata = baos.toByteArray();
			values.put(Constant.COLUMN_USERAVATAR, imagedata);
		}

		if (cursor.moveToFirst()) {
			int userId = cursor.getInt(0);
			db.update(Constant.TABLE_USERINFO, values, Constant.COLUMN_USERID + " = ?", new String[] { String.valueOf(userId) });
		} else {
			values.put(Constant.COLUMN_USERID, userInfo.getUserId());
			db.insert(Constant.TABLE_USERINFO, null, values);
		}
		if (cursor != null && !cursor.isClosed()) {
			cursor.close();
		}
	}

	public static void clearUserInfo(final Context c) {
		DatabaseHelpter dbHelper = DatabaseHelpter.getInstance(c);
		SQLiteDatabase db = dbHelper.getWritableDatabase();
		db.delete(Constant.TABLE_USERINFO, null, null);
	}
	
	public static Bitmap getUserAvatar(){
		if(userAvatar != null){
			return userAvatar;
		}
		return null;
	}
	
	public static void setUserAvatar(Bitmap mUserAvatar){
		userAvatar = mUserAvatar;
	}
}
